package com.lanxin.qx.initial;

import com.lanxin.qx.entity.QxFunction;
import com.lanxin.qx.service.IQxFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
//在项目启动时就将权限控制需要使用的数据存入redis中
public class QxFunctionsInitializer {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private IQxFunctionService qxFunctionService;

    @PostConstruct
    //在bean加载完成后自动执行该方法一次
    public void initFunctionsRedis(){
        List<QxFunction> functions = qxFunctionService.list();
        if(functions != null && !functions.isEmpty()){
            for(QxFunction function : functions){
                redisTemplate.opsForHash().put("functions","function"+function.getOid(),function);
            }
        }
        //redis中的数据保存24小时
        redisTemplate.expire("functions",24, TimeUnit.HOURS);
    }
}
